package com.rz.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rz.admin.entity.AdminRole;
import com.rz.admin.entity.Role;
import com.rz.admin.mapper.AdminRoleMapper;
import com.rz.admin.service.IAdminRoleService;
import com.rz.admin.service.IRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class AdminRoleService extends ServiceImpl<AdminRoleMapper, AdminRole> implements IAdminRoleService {

    @Resource
    private IRoleService roleService;

    @Override
    public List<AdminRole> getByAdminId(Integer adminId) {
        QueryWrapper<AdminRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id",adminId);
        return list(queryWrapper);
    }

    @Override
    public List<Integer> getRoleIdsByAdminId(Integer adminId) {
        Set<Integer> roleIds = new HashSet<>();
        List<AdminRole> adminRoles = getByAdminId(adminId);
        if(adminRoles!=null && adminRoles.size()>0){
            adminRoles.forEach(adminRole -> roleIds.add(adminRole.getRoleId()));
        }

        return new ArrayList<>(roleIds);
    }

    @Override
    public Collection<Role> getRolesByAdminId(Integer adminId) {
        List<AdminRole> adminRoles = getByAdminId(adminId);
        List<Integer> roleIds = new ArrayList<>();
        adminRoles.forEach(adminRole -> roleIds.add(adminRole.getRoleId()));
        return roleService.listByIds(roleIds);
    }
}
